# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from ..items import MatplotlibExamplesItem
class ExamplesSpider(scrapy.Spider):
    name = 'examples'
    allowed_domains = ['matplotlib.org']
    start_urls = ['http://matplotlib.org/examples/index.html']

    def parse(self, response):
        le = LinkExtractor(restrict_css='li.toctree-l2 a')
        for link in le.extract_links(response):
            yield scrapy.Request(link.url, callback=self.download)

    def download(self,response):

        href = response.css('a.reference::attr(href)').extract_first()
        url = response.urljoin(href)
        example =MatplotlibExamplesItem()
        example['file_urls'] =[url]
        return example